from django.db import models


# python 文件名.py makemigrations -----生成迁移文件
# python 文件名.py migrate -----执行迁移命令


class UserInfo(models.Model):
    """用户表"""
    phone = models.CharField(verbose_name="手机号", max_length=11, unique=True)
    # 用户登录时的随机字符串
    token = models.CharField(verbose_name="用户TOKEN", max_length=64, null=True, blank=True)
    username = models.CharField(verbose_name='昵称', max_length=64, default="默认用户")
    avatar = models.CharField(verbose_name='头像', max_length=128, null=True)
    password = models.CharField(verbose_name='用户密码', max_length=64, null=True, blank=True)
    status = models.BooleanField(verbose_name='用户状态', default=False)
    email = models.EmailField(verbose_name='邮箱', null=True, blank=True)


class Comment(models.Model):
    """评论记录表"""

    posts = models.ForeignKey(verbose_name="动态", to="Posts", on_delete=models.CASCADE)
    content = models.CharField(verbose_name="评论内容", max_length=255)
    user = models.ForeignKey(verbose_name="评论者", to="UserInfo", on_delete=models.CASCADE)
    create_date = models.DateTimeField(verbose_name="评论时间", auto_now_add=True)

    reply = models.ForeignKey(verbose_name="回复", on_delete=models.CASCADE, to="self", null=True, blank=True,
                              related_name="replys")
    depth = models.PositiveIntegerField(verbose_name="评论层级", default=1)
    root = models.ForeignKey(verbose_name="根评论", to="self", null=True, blank=True, on_delete=models.CASCADE,
                             related_name="roots")

    favor_count = models.PositiveIntegerField(verbose_name="评论赞数", default=0)


class CommentFavorRecord(models.Model):
    """
    评论赞记录
    """
    comment = models.ForeignKey(verbose_name='动态', to='Comment', on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name='点赞用户', to='UserInfo', on_delete=models.CASCADE)


class Posts(models.Model):
    """帖子表"""
    status_choices = (
        (1, '关注'),
        (2, '推荐'),
        (3, '体育'),
        (4, '热点'),
        (5, '财经'),
        (6, '娱乐'),
    )
    status = models.PositiveSmallIntegerField(verbose_name='状态', choices=status_choices, null=True, blank=True)
    title = models.CharField(verbose_name='标题', max_length=64)
    titlepic = models.CharField(verbose_name='第一张帖子图片', max_length=128)
    support_count = models.PositiveIntegerField(verbose_name="评论赞数", default=0)
    unsupport_count = models.PositiveIntegerField(verbose_name="评论踩数", default=0)
    share_num = models.PositiveIntegerField(verbose_name="分享个数", default=0)
    user = models.ForeignKey(verbose_name='发帖用户', to='UserInfo', on_delete=models.CASCADE)
    create_date = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    topic = models.ForeignKey(verbose_name='话题', to='Topic', on_delete=models.CASCADE, null=True, blank=True)


class PostsRecord(models.Model):
    """顶踩记录表"""
    status_choices = (
        (1, 'support'),
        (2, 'unsupport'),
    )
    status = models.PositiveSmallIntegerField(verbose_name='状态', choices=status_choices, default=1)
    posts = models.ForeignKey(verbose_name="帖子", to="Posts", on_delete=models.CASCADE)

    user = models.ForeignKey(verbose_name="点赞用户", to="UserInfo", on_delete=models.CASCADE)


class Follow(models.Model):
    status_choices = (
        (1, '互关'),
        (2, '关注'),
        (3, '粉丝'),
    )
    status = models.PositiveSmallIntegerField(verbose_name='状态', choices=status_choices, default=2)
    # status_follow = models.PositiveSmallIntegerField(verbose_name='当前用户关注状态', choices=status_choices, default=3)
    """关注表"""
    user = models.ForeignKey(verbose_name='当前用户', to='UserInfo', on_delete=models.CASCADE)
    user_follow = models.ForeignKey(verbose_name='被关注的用户', to='UserInfo', related_name='user_follow',
                                    on_delete=models.CASCADE)
    create_date = models.DateTimeField(verbose_name="关注时间", auto_now_add=True)


class PostsImages(models.Model):
    """帖子图片表"""
    images = models.CharField(verbose_name='第一张帖子图片', max_length=128)
    posts = models.ForeignKey(verbose_name="动态", to="Posts", on_delete=models.CASCADE)


class UserData(models.Model):
    """用户资料表"""
    sex_choices = (
        (0, '保密'),
        (1, '女'),
        (2, '男'),
    )
    emotion_choices = (
        (0, '未婚'),
        (1, '已婚'),
        (2, '保密'),
    )
    job_choices = (
        (0, 'IT'),
        (1, '老师'),
        (2, '主播'),
        (3, '设计'),
        (4, '其他'),
    )
    user = models.ForeignKey(verbose_name='当前用户', to='UserInfo', on_delete=models.CASCADE)
    sex = models.PositiveSmallIntegerField(verbose_name='性别', choices=sex_choices, default=0)
    birthday = models.DateTimeField(verbose_name="出生时间", auto_now_add=True)
    emotion = models.PositiveSmallIntegerField(verbose_name='情感', choices=emotion_choices, default=3)
    job = models.PositiveSmallIntegerField(verbose_name='职业', choices=job_choices, default=5)
    hometown = models.CharField(verbose_name="家乡", max_length=64, null=True, blank=True)


class UserOther(models.Model):
    """第三方登录"""
    status_choices = (
        (1, '微信'),
        (2, 'QQ'),
        (3, '新浪微博'),
    )
    status = models.PositiveSmallIntegerField(verbose_name='登录类型', choices=status_choices)
    openid = models.CharField(verbose_name="微信唯一标识", max_length=128)
    nickname = models.CharField(verbose_name='昵称', max_length=64)
    avatar = models.CharField(verbose_name='头像', max_length=256)
    user = models.ForeignKey(verbose_name='关联用户', to='UserInfo', on_delete=models.CASCADE, null=True, blank=True)


class News(models.Model):
    status_choices = (
        (1, 'text'),
        (2, 'images'),
        (3, 'files'),
    )
    """消息表"""
    receive_user = models.ForeignKey(verbose_name='被关注的用户', to='UserInfo', related_name='receive_user',
                                     on_delete=models.CASCADE)
    content = models.CharField(verbose_name="评论内容", max_length=255)
    user = models.ForeignKey(verbose_name='当前用户', to='UserInfo', on_delete=models.CASCADE)
    type = models.PositiveSmallIntegerField(verbose_name='消息类型', choices=status_choices, default=1)
    create_date = models.DateTimeField(verbose_name="消息发送时间", auto_now_add=True)
    unread = models.SmallIntegerField(verbose_name='未读数', default=0)


class Topic(models.Model):
    """话题表"""
    status_choices = (
        (1, '二次元'),
        (2, '萌宠'),
        (3, '潮流穿搭'),
        (4, '数码运动东'),
        (5, '灵异'),
        (6, '娱乐'),
    )

    title = models.CharField(verbose_name='话题名称', max_length=32)
    status = models.PositiveSmallIntegerField(verbose_name='话题分类', choices=status_choices)
    desc = models.CharField(verbose_name='话题描述', max_length=64)
    titlepic = models.CharField(verbose_name='封面', max_length=128)
